package com.qiguliuxing.dts.admin.web;

import javax.validation.constraints.NotNull;

import com.qiguliuxing.dts.admin.dao.InstrumentAll;
import com.qiguliuxing.dts.admin.dao.InstrumentAll;
import com.qiguliuxing.dts.admin.service.AdminInstrumentService;
import com.qiguliuxing.dts.admin.service.AdminInstrumentService;
import com.qiguliuxing.dts.admin.service.excel.ITestService;
import com.qiguliuxing.dts.db.domain.GmInstrument;
import com.qiguliuxing.dts.db.service.GmInstrumentMaintainService;
import com.qiguliuxing.dts.db.service.GmInstrumentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.admin.util.AuthSupport;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/instrument")
@Validated
public class AdminInstrumentController {
    private static final Logger logger = LoggerFactory.getLogger(AdminInstrumentController.class);

    @Autowired
    private GmInstrumentService GmInstrumentService;

    @Autowired
    private GmInstrumentMaintainService GmInstrumentMaintainService;

    @Autowired
    private AdminInstrumentService adminInstrumentService;

    @Autowired
    private ITestService itestService;

    /**
     * 查询商品
     * 查詢文檔
     * @param
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions("admin:instrument:list")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "查询")
    @GetMapping("/list")
    public Object list(String instrumentNumber,String instrumentName,String userDepartments,@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit/*,
                       @Sort @RequestParam(defaultValue = "overdue") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order*/) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材管理->查询,请求参数:instrumentNumber:{},instrumentName:{},page:{}", instrumentNumber, instrumentName,userDepartments,page);
        //需要区分数据权限，如果属于品牌商管理员，则需要获取当前用户管理品牌店铺
        /*   List<Integer> brandIds = null;*//*
         *//*if (adminDataAuthService.isBrandManager()) {*//*
         *//*        brandIds = adminDataAuthService.getBrandIds();
            logger.info("运营商管理角色操作，需控制数据权限，brandIds:{}",JSONObject.toJSONString(brandIds));

            if (brandIds == null || brandIds.size() == 0) {
                Map<String, Object> data = new HashMap<>();
                data.put("total", 0L);
                data.put("items", null);

                logger.info("【请求结束】商品管理->商品管理->查询,响应结果:{}", JSONObject.toJSONString(data));
                return ResponseUtil.ok(data);
          *//**//*  }*//*
         *//*     }*/

        return adminInstrumentService.list(instrumentNumber,instrumentName,userDepartments,page, limit/*, sort,order*/);
    }

/*    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
        return adminInstrumentService.catAndBrand();
    }*/

    /**
     * 编辑商品
     *
     * @param instrumentAll
     * @return
     */
    @RequiresPermissions("admin:instrument:update")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody InstrumentAll instrumentAll) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材列表->编辑,请求参数:{}", JSONObject.toJSONString(instrumentAll));

        return adminInstrumentService.update(instrumentAll);
    }

    @RequiresPermissions("admin:instrument:insertMany")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "导入")
    @GetMapping("/insertMany")
    public boolean addUser(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = itestService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  a;
    }

    /**
     * 删除器材
     *
     * @param instrument
     * @return
     */
    @RequiresPermissions("admin:instrument:delete")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody GmInstrument instrument) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材管理->删除,请求参数:{}", JSONObject.toJSONString(instrument));

        return adminInstrumentService.delete(instrument);
    }

    /*@RequiresPermissions("admin:instrument:insertMany")
    public Object insertMany(@RequestBody List<InstrumentAll> list){

        return
    }
*/
    /**
     * 添加商品
     *
     * @param instrumentAll
     * @return
     */
    @RequiresPermissions("admin:instrument:create")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody InstrumentAll instrumentAll) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName() + "] 器材管理->器材列表->新增,请求参数:{}", JSONObject.toJSONString(instrumentAll));
        GmInstrument instrument = instrumentAll.getInstrument();


        logger.info(instrument.toString());
        return adminInstrumentService.create(instrumentAll);
    }

    /**
     * 器材详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:instrument:read")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材管理->详情,请求参数,id:{}", id);
        logger.info("hehehehheheh");
        return adminInstrumentService.detail(id);
    }
    @RequiresPermissions("admin:instrument:read")
    @RequiresPermissionsDesc(menu = { "器材管理", "器材列表" }, button = "详情")
    @GetMapping("/more")
    public Object detail2(@NotNull Integer id) {
        logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 器材管理->器材管理->详情,请求参数,id:{}", id);
        logger.info("hehehehheheh");
        return adminInstrumentService.detail(id);
    }

}
